package com.example.hyw.szysteam.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.hyw.szysteam.POJO.ClassItem;
import com.example.hyw.szysteam.POJO.UserInfo;
import com.example.hyw.szysteam.R;
import com.example.hyw.szysteam.adapter.ClassAdapter;
import com.example.hyw.szysteam.presenter.GetClassPresenter;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUICenterGravityRefreshOffsetCalculator;
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout;

import java.util.ArrayList;

/**
 * Created by HYW on 2018/5/24.
 */

public class ClassActivity extends AppCompatActivity {
    QMUITopBar mTopBar;
    private CreateClassDialog createClassDialog;
    QMUIPullRefreshLayout refreshLayout;
    ClassAdapter classAdapter;
    GetClassPresenter getClassPresenter=new GetClassPresenter(this);
    ListView classes;
    JoinClassDialog joinClassDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class);
        initTopBar();
        initPullrefresh();
        getClassPresenter.getClass(UserInfo.getEmail());
    }

    private void initTopBar(){
        mTopBar=findViewById(R.id.topbar);
        mTopBar.setTitle("班级").setTextColor(getResources().getColor(R.color.myWhite));
        mTopBar.addLeftImageButton(R.drawable.ic_left,R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTopBar.addRightImageButton(R.drawable.ic_add,R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new QMUIBottomSheet.BottomListSheetBuilder(ClassActivity.this)
                        .addItem("添加好友")
                        .addItem("加入班级")
                        .addItem("创建班级")
                        .setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                            @Override
                            public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                                dialog.dismiss();
                                switch (position){
                                    case 0:
                                        Toast.makeText(getApplicationContext(),"添加好友",Toast.LENGTH_SHORT).show();
                                        break;
                                    case 1:
                                        showJoinClassDialog();
                                        break;
                                    case 2:
                                    default:
                                        if(UserInfo.getType()==1){
                                            showCreateClassDialog();
                                        }else {
                                            Toast.makeText(getApplicationContext(),"您不是老师不能创建班级",Toast.LENGTH_SHORT).show();
                                        }
                                }
                            }
                        })
                        .build()
                        .show();
            }
        });
    }

    private void initPullrefresh(){
        refreshLayout=findViewById(R.id.pull_refresh);
        refreshLayout.setRefreshOffsetCalculator(new QMUICenterGravityRefreshOffsetCalculator());
        refreshLayout.setOnPullListener(new QMUIPullRefreshLayout.OnPullListener() {
            @Override
            public void onMoveTarget(int offset) {

            }

            @Override
            public void onMoveRefreshView(int offset) {

            }

            @Override
            public void onRefresh() {
                getClassPresenter.getClass(UserInfo.getEmail());
            }
        });
    }

    public void initListView(final ArrayList<ClassItem> classItems){
        classes=findViewById(R.id.lv_classes);
        classes.setVisibility(View.VISIBLE);
        classAdapter=new ClassAdapter(ClassActivity.this,R.layout.item_class,classItems);
        classes.setAdapter(classAdapter);
        refreshLayout.finishRefresh();
        classes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle =new Bundle();
                bundle.putString("classid",classItems.get(i).getClassid());
                bundle.putString("classname",classItems.get(i).getClassname());
                Intent intent=new Intent();
                intent.putExtras(bundle);
                intent.setClass(ClassActivity.this,ClassInfoActivity.class);
                startActivity(intent);
            }
        });
    }

    public void noClass(){
        Toast.makeText(getApplicationContext(),"还没加入班级，赶快加入吧！",Toast.LENGTH_SHORT).show();
    }

    public void showCreateClassDialog(){
        createClassDialog=new CreateClassDialog();
        createClassDialog.show(getSupportFragmentManager(),"CreateClass");
    }

    public void showJoinClassDialog(){
        joinClassDialog=new JoinClassDialog();
        joinClassDialog.show(getSupportFragmentManager(),"JoinClass");
    }
}
